﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
    <h1>Send</h1>
    <?php
   	
		$send_Count = $_GET['send_Count'];
		$response = $_GET['response'];
		$user_id = $_GET['user_id'];
		$message_id = $_GET['message_id'];
		$response_id = $_GET['response_id'];
						
		$message_Send_Count = 0;


		$dbc = mysqli_connect( 'localhost' ,'root' , '', 'teresa' ) or die( 'Error connecting to MySQL server.');

		//--------------------------------------------------------------------------------------------------------------//
		//--------------------------------------------------------------------------------------------------------------//
		//--------------------------------------------------------------------------------------------------------------//
		$query = "SELECT * FROM teresa_message WHERE message_id = '$message_id'";					
		$data = mysqli_query( $dbc , $query) or die('ERROR FIND USER queerying database.');
		$row = mysqli_fetch_array( $data );
		$message = $row['message'];
		$subject = $row['subject'];
		$sender = $row['sender'];
		$maximum = $row['maximum'];
			
		if( $send_Count < $maximum ){
			$message_Maximum_Count = $send_Count;
		}else{
			$message_Maximum_Count = $maximum - $response;			
		}	
		//--------------------------------------------------------------------------------------------------------------//
		//--------------------------------------------------------------------------------------------------------------//
		//--------------------------------------------------------------------------------------------------------------//


		if( $message_Maximum_Count > 0 ){

			$query = "SELECT * FROM teresa_user WHERE user_id != '$user_id'";
			$data = mysqli_query( $dbc , $query) or die('ERROR FIND USER queerying database.');
			$row = mysqli_fetch_array( $data );
		
			//search user_list

			echo  '<p>Maximum_message : ' . $message_Maximum_Count . '</br></p>';

			while( $row = mysqli_fetch_array( $data ) ){			
				//insert location search logic...after we make useing bing map
				//insert category search logic...after discuses for category section
				
				$reciever = $row['email'];
				
				
				//find already recieved same message
				$query = "SELECT * FROM teresa_message WHERE response_id = '$response_id' AND reciever = '$reciever'";	
				$data2 = mysqli_query( $dbc , $query) or die('ERROR FIND USER queerying database.');
				$reciever_Message = mysqli_fetch_array( $data2 );
				

				if( !$reciever_Message ){								
					//break for maximum sending message
					if( $message_Send_Count < $message_Maximum_Count ){
						//send Message
		
	

						$query = "INSERT INTO teresa_message( sender, reciever, subject, message ,response_id )".
	  						"VALUES( '$sender' , '$reciever' , '$subject' ,'$message','$response_id'  )"; 
						mysqli_query( $dbc, $query ) or die('ERROR INSERT RESPONSE queerying database.');  
					
						echo '<p> Reciever : ' . $reciever . '</br></p>';


						$message_Send_Count = $message_Send_Count + 1;
					}else{						
						break;
					}
				}
			}
			echo '<p>Send '. $message_Send_Count . ' message</p>';
		}
		//close database
	    mysqli_close($dbc);	
		echo '<a href="main.php">Return to Main</a>'
    ?>
</body>
</html>
